<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="答题结果所属的问卷的id号" prop="questionnaireId">
      <el-input v-model="dataForm.questionnaireId" placeholder="答题结果所属的问卷的id号"></el-input>
    </el-form-item>
    <el-form-item label="答题结果对应的答卷人的id号" prop="answererId">
      <el-input v-model="dataForm.answererId" placeholder="答题结果对应的答卷人的id号"></el-input>
    </el-form-item>
    <el-form-item label="答卷结果(将答者所填的选项或者问题用字符串形式存储在answer_list中)" prop="answerList">
      <el-input v-model="dataForm.answerList" placeholder="答卷结果(将答者所填的选项或者问题用字符串形式存储在answer_list中)"></el-input>
    </el-form-item>
    <el-form-item label="答卷开始时的时间" prop="answerTime">
      <el-input v-model="dataForm.answerTime" placeholder="答卷开始时的时间"></el-input>
    </el-form-item>
    <el-form-item label="答卷结束时的时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="答卷结束时的时间"></el-input>
    </el-form-item>
    <el-form-item label="答卷人的ip地址" prop="ipAddress">
      <el-input v-model="dataForm.ipAddress" placeholder="答卷人的ip地址"></el-input>
    </el-form-item>
    <el-form-item label="此答卷是否提交(0为未提交，1为已提交)" prop="isFinish">
      <el-input v-model="dataForm.isFinish" placeholder="此答卷是否提交(0为未提交，1为已提交)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          questionnaireId: '',
          answererId: '',
          answerList: '',
          answerTime: '',
          endTime: '',
          ipAddress: '',
          isFinish: ''
        },
        dataRule: {
          questionnaireId: [
            { required: true, message: '答题结果所属的问卷的id号不能为空', trigger: 'blur' }
          ],
          answererId: [
            { required: true, message: '答题结果对应的答卷人的id号不能为空', trigger: 'blur' }
          ],
          answerList: [
            { required: true, message: '答卷结果(将答者所填的选项或者问题用字符串形式存储在answer_list中)不能为空', trigger: 'blur' }
          ],
          answerTime: [
            { required: true, message: '答卷开始时的时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '答卷结束时的时间不能为空', trigger: 'blur' }
          ],
          ipAddress: [
            { required: true, message: '答卷人的ip地址不能为空', trigger: 'blur' }
          ],
          isFinish: [
            { required: true, message: '此答卷是否提交(0为未提交，1为已提交)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/questionnaire/result/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.questionnaireId = data.result.questionnaireId
                this.dataForm.answererId = data.result.answererId
                this.dataForm.answerList = data.result.answerList
                this.dataForm.answerTime = data.result.answerTime
                this.dataForm.endTime = data.result.endTime
                this.dataForm.ipAddress = data.result.ipAddress
                this.dataForm.isFinish = data.result.isFinish
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/questionnaire/result/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'questionnaireId': this.dataForm.questionnaireId,
                'answererId': this.dataForm.answererId,
                'answerList': this.dataForm.answerList,
                'answerTime': this.dataForm.answerTime,
                'endTime': this.dataForm.endTime,
                'ipAddress': this.dataForm.ipAddress,
                'isFinish': this.dataForm.isFinish
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
